package com.ja.openchat.ui.chat.adapter.viewholder;

import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.R;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.ja.openchat.views.BubbleImageView;
import com.lqr.emoji.MoonUtils;
import com.zy.openchat.Constants;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.MessageContent;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.photo.PhotoSizeInfo;
import com.zy.openchat.util.Common;
import com.zy.openchat.util.WeChatImageUtils;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 图片尺寸展示高仿微信
 * 占位图-缩略图-原图
 */
@MessageContentType(value = TelegramManager.MessageType.MessageType_Photo)
@EnableContextMenu
public class ImageMessageContentViewHolder extends MediaMessageContentViewHolder {

    private static final String TAG = "ImageMessageContentView";
    @BindView(R.id.imageView)
    BubbleImageView imageView;
    @BindView(R.id.innerImage)
    ImageView innerImage;
    @BindView(R.id.imageContentTextView)
    TextView imageContentTextView;

    private String imagePath;

    public ImageMessageContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @Override
    public void onBind(UiMessage message) {
        MessageContent imageMessage = message.message.content;
        PhotoSizeInfo thumbnail = imageMessage.photo.messagePhoto();
        int width = Constants.MEDIA_MAX_WIDTH;
        int height = Constants.MEDIA_MAX_WIDTH;
        if(thumbnail!=null){
            int imageSize[] = WeChatImageUtils.getImageSizeByOrgSizeToWeChat((int) thumbnail.width, (int) thumbnail.height);
            width = imageSize[0] > 0 ? imageSize[0] : Constants.MEDIA_MAX_WIDTH;
            height = imageSize[1] > 0 ? imageSize[1] : Constants.MEDIA_MAX_WIDTH;
        }
        imageView.getLayoutParams().width = width;
        imageView.getLayoutParams().height = height;
        if (message.message.isMessagePhotoDownloaded()) {
            imagePath = message.message.getMessagePhotoFile().local.path;
        }else{
            TelegramManager.getTelegramManager().downloadFile(message.message.chat_id+"_" + message.message.id, message.message.getMessagePhotoFileId(), 0, TelegramManager.FileType.FileType_Message_Photo);
        }
        loadMedia(null, imagePath, innerImage);

        if(imageMessage.caption!=null&&!TextUtils.isEmpty(imageMessage.caption.text)){
            MoonUtils.identifyFaceExpressionAndAt(fragment.getContext()
                    , imageContentTextView,imageMessage.caption.text
                    , ImageSpan.ALIGN_BOTTOM
                    ,(Common.isEmpty(imageMessage.caption.entities))?null:imageMessage.caption.entities);
            imageContentTextView.setVisibility(View.VISIBLE);
        }else{
            imageContentTextView.setVisibility(View.GONE);
        }

    }

    @OnClick(R.id.imageView)
    void preview() {
        previewMM();
    }

    @Override
    protected void setSendStatus(MessageInfo item) {
        super.setSendStatus(item);
        if (item.messageType == TelegramManager.MessageType.MessageType_Photo) {
            if (item.isOutgoing()) {
                TelegramManager.MessageSendState sentStatus = item.sendState();
                if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Pending) {
                    imageView.setPercent(message.progress);
                    imageView.setProgressVisible(true);
                    imageView.showShadow(true);
                } else if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Fail) {
                    imageView.setProgressVisible(false);
                    imageView.showShadow(false);
                } else if (sentStatus == TelegramManager.MessageSendState.MessageSendState_Success) {
                    imageView.setProgressVisible(false);
                    imageView.showShadow(false);
                }
            } else {
                imageView.setProgressVisible(false);
                imageView.showShadow(false);
            }
        }
    }

}
